import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:my_shop/cache_event_identification.dart';
import 'package:my_shop/load_my_goods.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({
    Key? key,
  }) : super(key: key);

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  final TextEditingController userNameController = TextEditingController();
  final TextEditingController passwordController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color(SpUtil.getInt(Constant.mainColor) ?? MyColor.mainColor.value),
        title: const Text('Login'),
      ),
      body: Container(
        padding: const EdgeInsets.symmetric(horizontal: 30),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Card(
              color: Color(SpUtil.getInt(Constant.mainColor) ?? MyColor.mainColor.value),
              child: Container(
                padding: const EdgeInsets.all(16),
                child: Column(
                  children: [
                    const Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          'Welcome to Login',
                          style: TextStyle(
                              fontSize: 18, fontWeight: FontWeight.bold),
                        )
                      ],
                    ),
                    const SizedBox(height: 16),
                    TextField(
                      controller: userNameController,
                      decoration: InputDecoration(
                        labelText: 'username:',
                        hintText: 'Please input username',
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(8),
                        ),
                        contentPadding: const EdgeInsets.symmetric(
                          vertical: 12,
                          horizontal: 16,
                        ),
                      ),
                    ),
                    const SizedBox(height: 16),
                    TextField(
                      controller: passwordController,
                      decoration: InputDecoration(
                        labelText: 'password:',
                        hintText: 'Please input password',
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(8),
                        ),
                        contentPadding: const EdgeInsets.symmetric(
                          vertical: 12,
                          horizontal: 16,
                        ),
                        suffixIcon: const Icon(Icons.lock),
                      ),
                      obscureText: true,
                    ),
                    const SizedBox(height: 30),
                    Container(
                      decoration: BoxDecoration(
                        color: const Color(0xFFF8961E),
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: Row(
                        children: [
                          Expanded(
                            child: TextButton(
                              onPressed: login,
                              child: const Text(
                                'Login',
                                style: TextStyle(color: Colors.black),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  void login() {
    List<String> userNameList = [];
    List<String> passwordList = [];
    List<UserModel> allUserAccount = SpUtil.getObjList<UserModel>(
            Constant.userModelList,
            (v) => UserModel.fromJson(v as Map<String, dynamic>)) ??
        [];
    for (var model in allUserAccount) {
      userNameList.add(model.userName ?? '');
      passwordList.add(model.password ?? '');

      if (model.userName == userNameController.text &&
          model.password == passwordController.text) {
        Fluttertoast.showToast(msg: "Login in success");
        SpUtil.putBool(Constant.isLoginStatus, true);
        SpUtil.putString(Constant.isLoginStatusUserName, model.userName??'');

        Navigator.pop(context, true);
        return;
      }
    }

    if (userNameList.contains(userNameController.text)) {
      Fluttertoast.showToast(msg: 'Password error');
    } else {
      Fluttertoast.showToast(msg: 'The current user does not exist');
    }
  }
}
